﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VOL.Core.DBManager;
using VOL.Core.Extensions;
using VOL.Core.Filters;
using VOL.Demo.IRepositories;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.Controllers.DbTest
{
    [JWTAuthorize,ApiController]
    [Route("api/dbTest")]
    
    public class DbTestController : Controller

    {

        private readonly IMechanicaRepository _mechanicaRepository;
        private readonly IMechanica_records2Repository _mechanica_Records2Repository;

        public DbTestController(
            IMechanicaRepository mechanicaRepository, IMechanica_records2Repository Mechanica_Records2Repository)
        {
            this._mechanicaRepository = mechanicaRepository;
            this._mechanica_Records2Repository = Mechanica_Records2Repository;
        }
        

        [Route("ef"),HttpGet]
        public async Task<IActionResult> EF(string Sbmc)
        {
            List<Mechanica> mechanicas1 =  _mechanicaRepository.FindAsIQueryable(x => x.sbxh == "锻造设备" && x.sbmc == Sbmc).ToList();

            List<Mechanica> mechanicas2 =await _mechanicaRepository.FindAsIQueryable(x => x.sbxh == "锻造设备" && x.sbmc == Sbmc).ToListAsync();
            if (!string.IsNullOrEmpty(Sbmc))
            {

            }

            _mechanicaRepository.Update(mechanicas1[0], x => new { x.sbtp }, true);

            _mechanicaRepository.UpdateRange(mechanicas1,x => new {x.sbtp,x.sbbm},true);

            //var mechanicas5 = new Mechanica()
            //{
            //   sbtp="232"
         
            //};
            //mechanicas5.SetCreateDefaultVal();
            //_mechanicaRepository.Add(mechanicas5, true);






            List<Mechanica> mechanicas3 = await _mechanicaRepository.FindAsIQueryable(x => x.sbxh == "锻造设备").WhereNotEmpty(x=>x.sbmc,Sbmc,Core.Enums.LinqExpressionType.Contains).ToListAsync();



            List<Mechanica> mechanicas4 = (from a in _mechanicaRepository.DbContext.Set<Mechanica>()
                                              join b in _mechanicaRepository.DbContext.Set<Mechanica_records2>()
                                              on a.id equals b.id
                                              //where b.Role_Id == roleId && b.AuthValue != ""
                                              orderby a.id
                                              select new Mechanica
                                              {
                                                  id = a.id,
                                                  sbmc = a.sbmc,
                                                  sbbm = a.sbbm,
                                                  sbxh = a.sbxh,
                                                  sbtp = b.sbtp2
                                              }).ToList();

            List<Mechanica> mechanicas = _mechanicaRepository.FindAsIQueryable(x => true).Include(x => x.Mechanica_records2).ToList();
             await Task.CompletedTask;
            return Content("Ef");
        }
        [Route("dapper"),HttpGet]
        public async Task<IActionResult> Dapper(string Sbmc)
        {
            string sql = "select * from Mechanica where sbmc=@Sbmc";
            List<Mechanica> mechanica1 = DBServerProvider.SqlDapper.QueryList<Mechanica>(sql, new { Sbmc });
            await Task.CompletedTask;
            return Content("Dapper");
        }
    }

   
}
